package com.wake.mycomposebook.ui.home.myhome

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FiberManualRecord
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wake.mycomposebook.utils.showLog
import com.wake.theme.modifier.*


/**
 *@author created by WA03675
 *@date  2022-04-14 10:46
 *@des
 */

@Composable
fun MyColorPallet(modifier: Modifier, onPalletClick: (ColorPallet) -> Unit) {
    val colors = ColorPallet.values()

    Box(
        modifier = Modifier.fillMaxWidth(),
    ) {
        Column(
            modifier
                .width(150.dp)
                .background(MaterialTheme.colorScheme.primary)
        ) {
            colors.forEach {
                MyColorPalletItem(it) {
                    onPalletClick.invoke(it)
                }
            }
        }
    }
}

/**
 *如何获取颜色
 * @param colorPallet
 * @param clickListener
 */
@Composable
fun MyColorPalletItem(colorPallet: ColorPallet, clickListener: () -> Unit) {
//    Button(
//        modifier = Modifier.fillMaxWidth(),
//        onClick = clickListener
//    ) {
//        Icon(
//            imageVector = Icons.Filled.FiberManualRecord,
//            contentDescription = null,
//            modifier = Modifier
//                .padding(10.dp, 0.dp)
//                .width(30.dp),
//            tint = getColor(colorPallet)
//        )
//        Text(text = colorPallet.name, color = MaterialTheme.colorScheme.onPrimary)
//    }
    Row(
        modifier = Modifier
            .padding(4.dp, 10.dp)
            .fillMaxWidth()
            .clickable { clickListener.invoke() },
//        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Icon(
            imageVector = Icons.Filled.FiberManualRecord,
            contentDescription = null,
            tint = getColor(colorPallet)
        )
        Text(text = colorPallet.name)

    }
}

fun getColor(colorPallet: ColorPallet): Color {
    val color = when (colorPallet) {
        ColorPallet.GREEN -> green700
        ColorPallet.BLUE -> blue700
        ColorPallet.ORANGE -> orange700
        ColorPallet.PURPLE -> purple700
        else -> green700
    }
    return color
}

@Composable
@Preview
fun PreviewMyColorPallet() {
    Box(modifier = Modifier.fillMaxSize()) {
        MyColorPallet(modifier = Modifier.align(Alignment.TopEnd)) {
            showLog(it.name)
        }
    }
}